package com.yixun.qfbao.hander;

import java.util.Calendar;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.yixun.qfbao.apidto.GetTimeIntervalReqDto;
import com.yixun.qfbao.apidto.GetTimeIntervalResDto;
import com.yixun.qfbao.dto.base.BaseRequestDTO;
import com.yixun.qfbao.dto.base.BaseResponseDTO;
import com.yixun.qfbao.hander.base.CommonAbstractHander;
import com.yixun.qfbao.vo.GetTimeIntervalReqVo;
import com.yixun.qfbao.xf.model.enums.ResultEnum;

@Service("getTimeIntervalHander")
public class GetTimeIntervalHander extends CommonAbstractHander {
	private static Logger logger= LoggerFactory.getLogger(GetTimeIntervalHander.class);


    @SuppressWarnings("rawtypes")
    @Override
    public BaseResponseDTO doBusiness(BaseRequestDTO reqDto) {
        GetTimeIntervalReqVo req = (GetTimeIntervalReqVo)reqDto;
        GetTimeIntervalReqDto GetTimeIntervalReqDto = req.getParams();
        BaseResponseDTO<GetTimeIntervalResDto> res = new BaseResponseDTO<GetTimeIntervalResDto>();
        GetTimeIntervalResDto resDto = new GetTimeIntervalResDto();
        try {
        	//拼接时间
        	String beginDate = "";
        	String endDate = "";
        	Calendar calendar = Calendar.getInstance();
        	if("1".equals(GetTimeIntervalReqDto.getTimeType())) {
        		//昨日
        		calendar.add(Calendar.DAY_OF_MONTH, -1);
        		int year = calendar.get(Calendar.YEAR);
        		int month = calendar.get(Calendar.MONTH) + 1;
        		int day = calendar.get(Calendar.DAY_OF_MONTH);
        		beginDate = year+"-"+(month<10?("0"+month):month)+"-"+(day<10?("0"+day):day);
        		endDate = year+"-"+(month<10?("0"+month):month)+"-"+(day<10?("0"+day):day);
        	}else if("2".equals(GetTimeIntervalReqDto.getTimeType())) {
        		//今日
        		int year = calendar.get(Calendar.YEAR);
        		int month = calendar.get(Calendar.MONTH) + 1;
        		int day = calendar.get(Calendar.DAY_OF_MONTH);
        		beginDate = year+"-"+(month<10?("0"+month):month)+"-"+(day<10?("0"+day):day);
        		endDate = year+"-"+(month<10?("0"+month):month)+"-"+(day<10?("0"+day):day);
        	}else if("3".equals(GetTimeIntervalReqDto.getTimeType())) {
        		//本周
        	    // 获取星期一开始时间戳
        		int day_of_week = calendar.get(Calendar. DAY_OF_WEEK) - 1;
        	    if (day_of_week == 0) {
        	        day_of_week = 7 ;
        	    }
        	    calendar.add(Calendar.DATE , -day_of_week + 1 );  
        		int year = calendar.get(Calendar.YEAR);
        		int month = calendar.get(Calendar.MONTH) + 1;
        		int day = calendar.get(Calendar.DAY_OF_MONTH);
        		beginDate = year+"-"+(month<10?("0"+month):month)+"-"+(day<10?("0"+day):day);
        		//周日
        		day_of_week = calendar.get(Calendar. DAY_OF_WEEK) - 1;
        	    if (day_of_week == 0) {
        	        day_of_week = 7;
        	    }
        	    calendar.add(Calendar.DATE , -day_of_week + 7 ); 
        		year = calendar.get(Calendar.YEAR);
        		month = calendar.get(Calendar.MONTH) + 1;
        		day = calendar.get(Calendar.DAY_OF_MONTH);
        		endDate = year+"-"+(month<10?("0"+month):month)+"-"+(day<10?("0"+day):day);
        	}else if("4".equals(GetTimeIntervalReqDto.getTimeType())) {
        		//本月
        		calendar.set(Calendar.DATE, 1);//把日期设置为当月第一天
        		int year = calendar.get(Calendar.YEAR);
        		int month = calendar.get(Calendar.MONTH) + 1;
        		int day = calendar.get(Calendar.DAY_OF_MONTH);
        		beginDate = year+"-"+(month<10?("0"+month):month)+"-"+(day<10?("0"+day):day);
        		
        		calendar.roll(Calendar.DATE, -1);//日期回滚一天，也就是最后一天
        		year = calendar.get(Calendar.YEAR);
        		month = calendar.get(Calendar.MONTH) + 1;
        		day = calendar.get(Calendar.DAY_OF_MONTH);
        		endDate = year+"-"+(month<10?("0"+month):month)+"-"+(day<10?("0"+day):day);
        	}
        	
        	resDto.setBeginDate(beginDate);
        	resDto.setEndDate(endDate);
        	resDto.setResult(ResultEnum.SUCCESS.getNo());
        } catch (Exception e) {
            logger.error("GetTimeIntervalHander error: {}", e);
            resDto.setResult(ResultEnum.FAIL.getNo());
            resDto.setResultMsg("计算快速时间区间失败");
        }
        res.setResData(resDto);
        res.setResult(resDto.getResult());
        return res;
    }
}
